perm filename A107.TEX[106,RWF]1 blob
sn#807808 filedate 1985-11-20 generic text, type C, neo UTF8
COMMENT ā VALID 00002 PAGES
C REC PAGE DESCRIPTION
C00001 00001
C00002 00002 \magnification\magstephalf
C00012 ENDMK
Cā;
\magnification\magstephalf
\input macro.tex
\def\today{\ifcase\month\or
January\or February\or March\or April\or May\or June\or
July\or August\or September\or October\or November\or December\fi
\space\number\day, \number\year}
\baselineskip 14pt
\font\rmn=amr9
\rm
\line{\sevenrm a107.tex[106,phy] \today\hfill}
\bigskip
\line{{\bf Strings} (Assumes Arrays, {\tt CHAR}, procedures)\hfil}
\line{\it ``Tie a Little String Around Your Finger''\hfil}
Our written languages are based on sequences of characters: words, sentences,
paragraphs, books, titles, part numbers, license plates, mathematical formulas,
even computer programs. In Pascal, values like these are kept in arrays of
characters. Such arrays are called strings.
Things we might want a computer to do with strings:
\smallskip
\disleft 25pt:
(1):Matching. Let's say that somewhere in a large manuscript I remember
using the phrase ``begging the question''. I've learned that I~misused the
term, so I~want to find that place. Probably to find ``begging'' will suffice.
\smallskip
\disleft 25pt:
(2):Substitution. I have developed a program using shorthand names for the
variables. Now I~want to change {\tt GW} to {\tt GROSSWAGES}
throughout the program.
\smallskip
\disleft 25pt:
(3):Alphabetization. I have an array of strings, each containing the name,
address, and telephone number of one employee. I~want to arrange them in
alphabetical order by name, to print a company directory.
\smallskip
\disleft 25pt:
(4):Formatting. I want to rearrange my manuscript so that paragraphs are
separated by blank lines, and lines are stretched by insertion of spaces to
give straight margins on left and right. I~also want to center the chapter
headings. Or I~want to space and indent a Pascal program consistently.
\smallskip
\disleft 25pt:
(5):Interpretation. I want my program to read phrases like ``three thousand
two hundred eighty'' on a check, or ``gale force winds'' on a meteorological
report and translate them into numerical equivalents.
\smallskip
\disleft 25pt:
(6):Translation. I want to provide a word-by-word translation from Spanish
to English.
\medskip
\line{\it String Types\hfil}
A string type is a type name that has been defined to be [RWF: define ``packed'']
{\obeylines\obeyspaces\let =\ \tt
PACKED ARRAY [1..N] OF CHAR
}
\noindent
for some integer constant {\tt N}. In this text, we will include the value
of~{\tt N} in the names we use for string types; we might define
{\obeylines\obeyspaces\let =\ \tt
TYPE STRING8O = PACKED ARRAY[1..80] OF CHAR;
}
\noindent
Such types can be used as types of program variables:
{\obeylines\obeyspaces\let =\ \tt
VAR LINE1,LINE2: STRING80;
}
\noindent
or of subprogram parameters:
{\obeylines\obeyspaces\let =\ \tt
PROCEDURE CENTER (INPUTLINE: STRING80; VAR OUTPUTLINE: STRING80);
}
\noindent
They may not, unfortunately, be used as types of function results in Standard
Pascal.
[Mention string assignment]
We can do with strings all the operations implicit in an array of
characters. To find if a word ends in a vowel we might use the program fragment:
\vfill\eject
{\obeylines\obeyspaces\let =\ \tt
...
...
VAR WORD: STRING20;
C: CHAR;
...
I:=1;
WHILE WORD[I+1] <> '{\spa}' DO I:=I+1;
(* NOW WORD[I+1] IS BLANK *)
C:=WORD[I];
IF (C='A') OR (C='E') OR (C='I') OR (C='O') OR (C='U')
THEN S1 ELSE S2
}
\noindent
Additionally, Pascal includes some operations automatically defined for string
types. If {\tt X} and~{\tt Y} are of the
{\it same\/} string type, they can be tested for
equality and for alphabetical order:
{\obeylines\obeyspaces\let =\ \tt
VAR X,Y: STRING20;
...
...
IF X=Y THEN WRITE ('SAME WORD')
ELSE IF X<Y THEN WRITE ('X COMES BEFORE Y')
ELSE WRITE ('Y COMES BEFORE X')
}
The tests of alphabetical order are based on the ordering of the
characters, so the results are probably not what you want if the strings
mix capital with lower case letters. In the ASCII code, for example,
{\tt 'ZEBRA{\spa\spa\spa'}} comes before {\tt 'aardvark'},
because an upper case letter~{\tt z} comes
before a lower case letter~{\tt A}.
You can read to a string variable: [Not standard Pascal]
{\obeylines\obeyspaces\let =\ \tt
READ(WORD)
}
\noindent
reads one line of an input text file, adds spaces on the right if necessary,
and assigns the resulting string to {\tt WORD}. If the line is longer than
{\tt WORD}, the command reads only enough to fill {\tt WORD}.
The string constants of a particular length
belong to all the string types of the same length; you can
assign
{\obeylines\obeyspaces\let =\ \tt
WORD:='ABCDEFGHIJKLMNOPQRST';
}
\noindent
if {\tt WORD} is of type {\tt STRING20}.
You can print a string.
{\obeylines\obeyspaces\let =\ \tt
WRITELN(WORD) {\rm{prints}} ABCDEFGHIJKLMNOPQRST
}
The list above contains all of the built-in operations in strings in Pascal.
Comparing it to the long list of functions, operators, and relations on
numbers, you can see that Pascal was designed more to work on numbers than
on strings. Many Pascal implementations, though, have extensions (``string
packages'') to do more, and programmers can design their own procedures to
do common string manipulations. Here is one to capitalize letters in
string {\tt LINE1}, putting the result in {\tt LINE2}:
[Does not meet Pascal Standard]
\vfill\eject
{\obeylines\obeyspaces\let =\ \tt
PROCEDURE CAPITALIZE(LINE1:STRING80;VAR LINE2: STRING80);
VAR I:INTEGER;
BEGIN
FOR I:=1 TO 80 DO
IF ('a'<=LINE1[I]) AND (LINE1[I]<='z') THEN
LINE2[I]:=CHR(ORD(LINE1[I])-ORD('a')+ORD('A'))
ELSE LINE2[I]:=LINE1[I]
END;
}
\bigskip
\line{\copyright 1984 Robert W. Floyd;
First draft (not published) March 28, 1984\hfil}
%revised: Date; subsequently revised.\hfill}
\bye